let name = ''
import {createTeacherClassWrap} from './class.js'
import {MyClassHistoryListener} from './listener.js'
// file-class component listener
let studentShowListListener = () => {
    $(".student-xiangxia").each(function (index) {
        $(this).on("click", (e) => {
            if (e.target.style.transform === "rotate(360deg)") {
                e.target.style.transform = "rotate(180deg)";
            } else {
                e.target.style.transform = "rotate(360deg)";
            }
            $(".component-file-student-list2").slideToggle();
        });
    });
};

let fileBackListener = () => {
    $(".file-back")[0].addEventListener("click", (e) => {
        $(".component-wrap-file")[0] && $(".component-wrap-file")[0].remove();
        createTeacherClassWrap();
    });
};

// main page listener
let classHistoryOpenListener = () => {
    $(".openClassHistory").each(function (index) {
        $(this).on("click", async (e) => {
            let res = await ajax('student_teacher_curriculum_query',{
                studentid : e.target.dataset.userid,
                state : 2
            },'post')
            if(res?.data.length){
                createClassHistory(res.data);
                $(".component-wrap")[0] && $(".component-wrap")[0].remove();
                $(".component-wrap-file")[0] && $(".component-wrap-file")[0].remove();
            }else{
                Dreamer.error('暂时没有课程记录...')
            }
        });
    });
};

// history class create
let createHistoryList = (res) => {
    let weekarray = ['星期一', '星期二', '星期三', '星期四', '星期五', '星期六', '星期日'];
    let list = document.createElement("div");
    list.className = "history-btm wow animated fadeInDown";
    name = res.teachername
    list.innerHTML = `
        <div style='border-right:1px solid #D8D8D8;flex-direction: column;'>
            <div>${res.cuyear}.${res.cumonth}.${res.cuday}</div>
            <div>${weekarray[parseInt(res.weekji) - 1]}${res.timeslot}</div>
        </div>
        <div style='border-right:1px solid #D8D8D8'>
            ${res.tutorialname}教材
        </div>
        <div style='border-right:1px solid #D8D8D8'>第${res.whatclass}课</div>
        <div style='border-right:1px solid #D8D8D8'>${res.title}</div>
        <div style='border-right:1px solid #D8D8D8'>${res.pagequantity}</div>
        <div style='border-right:1px solid #D8D8D8'>${res.keysentences}</div>
        <div style='border-right:1px solid #D8D8D8'>${res.grammaritem}</div>
        <div style='border-right:1px solid #D8D8D8'>
            ${res.task}
        </div>
        <div style='border-right:1px solid #D8D8D8'>${res.startnexttime}</div>
        <div style='border-right:1px solid #D8D8D8'>${res.teacherreflections}</div>
    `;
    return list;
};

// file-class component student list create
let createClassHistory = async (res) => {
    let classHistory = document.createElement("div");
    classHistory.className = "compone-class-history";
    classHistory.innerHTML = `
        <i class='iconfont icon-xiang-zuo back-class wow animated fadeInDown'></i>
        <div class='history-top wow animated fadeInDown'>
            <div>开始时间</div>
            <div>教程名</div>
            <div>第几课</div>
            <div>标题</div>
            <div>页数</div>
            <div>重点句型</div>
            <div>语法项目</div>
            <div>作业</div>
            <div>下次何处开始</div>
            <div>老师感想</div>
        </div>
    `;
    // let res = await ajax('student_teacher_curriculum_query',{
    //     teacherid : 17,
    //     state : 2
    // },'post')
    // console.log(res)
    for (let i = 0; i < res.length; i++) {
        let list = createHistoryList(res[i]);
        classHistory.appendChild(list);
    }
    $(".body")[0].appendChild(classHistory);
    MyClassHistoryListener();
    $('.back-class')[0].innerHTML = `
        <div style='width:100%;text-align:center'>${name}的课程记录</div>
    `
};

export {
    studentShowListListener,
    fileBackListener,
    classHistoryOpenListener
}